package com.example.algorithm.stacklist;

import java.util.Stack;

/**
 * @author : zhy
 * @since : 2022/7/27
 */
public class ReverseCount {

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String a:tokens)
        {
            if(a=="+"||a=="-"||a=="/"||a=="*") {
                int count=0;
                Integer pop1 = stack.pop();
                Integer pop2 = stack.pop();
                if(a=="+") {
                    count=pop1+pop2;
                }else if(a=="-") {
                    count=pop2-pop1;
                }else if(a=="*") {
                    count=pop1*pop2;
                }else {
                    count=pop2/pop1;
                }
                stack.push(count);
            } else {
                stack.push(Integer.valueOf(a));
            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        ReverseCount count = new ReverseCount();
        String []xie= {"2","1","+","3","*"};
        System.out.println(count.evalRPN(xie));
    }
}
